<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>陕西人才知识管理</title>
<script type="text/javascript">
	var urL;
	var roleid;
	
	//查询符合条件的用户
	function searchRolePermission() {
		$('#dg').datagrid('load', {
			rolename : $('#rolename').val(),
		});
	}
	
	
	function choosePermission(index) {
		var rows = $('#dg').datagrid('getRows');
		var row = rows[index];
		/* $('#dg').datagrid('selectRow',index);
		var row = $('#dg').datagrid('getSelected'); */
		var rolename = row.rolename;
		$("#dlg").dialog("open").dialog("setTitle", rolename+"权限信息");
         cleanRows();
		 roleid = row.roleid;
		ajaxPermissionId(roleid);
	}
	//请求权限信息
	function ajaxPermissionId(index){
		
		$.ajax({
			url:"getRolePermissionList",
			type:'POST',
			async:true,
			data:{roleid:index},
			success:function(value){
				var data = eval(value);
				if(value !="")
					selectRows(data);
			}
		});
	}
	
	//查询角色权限并选中
	function selectRows(values)
	{
		
		var rows = $("#rolepermission").datagrid("getRows");
		for(var i=0;i<rows.length;i++)
		{
		      var rowId = rows[i].permissionid;
		      for(var j=0;j<values.length;j++)
		      {
		    	
		        if(rowId== values[j].permissionid)
		        {
		          var index = $("#rolepermission").datagrid("getRowIndex",rows[i]);
		          $("#rolepermission").datagrid("checkRow",index);
		          break;
		        }
		      }
		    }
	}
	
	function cleanRows(){
		
		$("#rolepermission").datagrid("uncheckAll","none");
	}
	
	function saveRolePermission() {
		var selectedRows = $("#rolepermission").datagrid('getSelections');
		var json =[];
		var loc;
		
		 if(selectedRows.length==0){
			
			loc ={
		    		  "roleid":roleid
			     }
	  		json.push(loc);
		}
		else{
			
			$.each(selectedRows, function (i) { 
				
			      loc= { 
			    		  "permissionid": selectedRows[i].permissionid,
			    		  "roleid":roleid
			      		};  
			  		json.push(loc);
			  });
		}
 		
		  json = JSON.stringify(json);
			$.ajax({
	            type: "POST",
	            url: "addRolePermission?",
	            contentType: "application/json;charset=utf-8", //必须有不然请求失败
	            dataType: "json", 
	            data: json,
	            success: function (result) {
	            	if(result){
						$.messager.alert("系统提示","保存成功");
						$("#dlg").dialog("close");
						$("#dg").datagrid("reload");
					}else{
						$.messager.alert("系统提示","保存失败");
						return;
					}
	            }
	        });
	}

	function formatOper(val, row, index) {
		
		return '<a href="#" onclick="choosePermission('+index+ ')">选择权限</a>';
	}
	
	//关闭对话框
	function closeMessageDialog() {
		$("#dlg").dialog("close");
	}
</script>
<link rel="stylesheet" type="text/css"
	href="../../easyui-1.5/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="../../easyui-1.5/themes/icon.css" />
<script type="text/javascript" src="../../easyui-1.5/jquery.min.js"></script>
<script type="text/javascript"
	src="../../easyui-1.5/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="../../easyui-1.5/locale/easyui-lang-zh_CN.js"></script>
</head>
<body style="margin: 5px">
	<table id="dg" title="权限分配" class="easyui-datagrid" 
		striped="true"  fitColumns="true"
	    pagination="true" rownumbers="true"
		url="getRolePermission" fit="true" toolbar="#tb" style="height:300px">
		<thead>
			<tr>
				<th field="cb" checkbox="true"></th>
				<th field="roleid" width="100" align="center" hidden="true"></th>
				<th field="rolename" width="100" align="center">角色名称</th>
				<th field="rolepermission" width="200" align="center">角色权限</th>
				<th field="_operate" width="50" align="center" formatter="formatOper">操作</th>
			</tr>
		</thead>
	</table>
		
	<div id="tb" style="padding-bottom: 15px; padding-top: 15px">
		<div>
			<form method="post">
				&nbsp;角色名称：&nbsp;<input type="text" name="rolename"
					id="rolename" size="20" style="width:300px" /> 
					<a href="javascript:searchRolePermission()" class="easyui-linkbutton" iconCls="icon-search" plain="true">查询</a>
			</form>
		</div>
	</div>
	<div id="dlg" class="easyui-dialog"
		style="width: 350px; height: 450px;" closed="true"
		buttons="#dlg-buttons">
		<form id="fm" method="post" style="height: 100%">
		<table  id="rolepermission"  title="权限名称" class="easyui-datagrid" fitColumns="true"
			striped="true"  rownumbers="true"
			url="getRPermissionList" fit="true" >
			<thead>
				<tr>
					<th field="cb" checkbox="true">全选</th>
					<th field="permissionid" width="100" align="center" hidden="true"></th>
					<th field="permissionname" width="100" align="center">权限名称</th>
				</tr>
			</thead>
		</table>
		</form>
	</div>

	<div id="dlg-buttons">
		<a href="javascript:saveRolePermission()" class="easyui-linkbutton"
			iconCls="icon-ok">保存</a> 
			<a href="javascript:closeMessageDialog()"
			class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>

</body>
</html>